//
//  TInventoryDAL.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/20.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "TInventoryDAL.h"
#import "CSFMDBTool.h"

#import "CSGXInventoryManagerBLL.h"

@implementation TInventoryDAL

+ (TInventoryModel *)loadModelWithDict:(NSDictionary *)dict {
    TInventoryModel *model = [[TInventoryModel alloc] init];
    model.iId = [dict[@"iId"] intValue];
    model.iName = dict[@"iName"];
    model.iCoNo = dict[@"iCoNo"];
    model.iUnit = dict[@"iUnit"];
    model.iInventoryCount = dict[@"iInventoryCount"];
    model.iCreateUserNo = dict[@"iCreateUserNo"];
    model.iCreateTime = dict[@"iCreateTime"];
    model.iIsChecked = [dict[@"iIsChecked"] integerValue];
    model.iCheckedTime = dict[@"iCheckedTime"];
    model.iRemark = dict[@"iRemark"];
    
    return model;
}

+ (NSArray<NSDictionary *> *)loadDictArrWithSql:(NSString *)sql {
    return [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
}

//可去
+ (NSArray<NSDictionary *> *)loadDictArrWithCommNo:(NSString *)commNo {
    NSString *sql = [NSString stringWithFormat:@"select * from  tInventoryInfo where iCoNo='%@'; ", commNo];
    return [self loadDictArrWithSql:sql];
}

+ (NSDictionary *)loadDictWithSql:(NSString *)sql {
    return [self loadDictArrWithSql:sql][0];
}

+ (NSArray<TInventoryModel *> *)loadListWithDictArr:(NSArray<NSDictionary *> *)arr {
    NSMutableArray<TInventoryModel *> *modelList = [NSMutableArray<TInventoryModel *> array];
    for (NSDictionary *dict in arr) {
        [modelList addObject:[self loadModelWithDict:dict]];
    }
    return modelList.copy;
}

+ (NSArray<TInventoryModel *> *)loadListWithCommNo:(NSString *)commNo {
    NSArray<NSDictionary *> *arr = [self loadDictArrWithCommNo:commNo];
    if (!arr || arr.count == 0) {
        NSLog(@"error: loadListWithCommNo failed!");
        return nil;
    }
    return [self loadListWithDictArr:arr];
}

+ (TInventoryModel *)loadModelWithCommNo:(NSString *)commNo {
    return [self loadListWithCommNo:commNo][0];
}


#pragma mark - update

+ (NSInteger)updateDbWithInventoryModel:(TInventoryModel *)inventoryModel {
    NSString *sql = [NSString stringWithFormat:@"update tInventoryInfo set "
                     "iCoNo = '%@', "
                     "iName = '%@', "
                     "iUnit = '%@', "
                     "iInventoryCount = '%@', "
                     "iCreateUserNo = '%@', "
                     "iCreateTime = '%@', "
                     "iIsChecked = %lu, "
                     "iCheckedTime = '%@', "
                     "iRemark = '%@' "
                     "where iId = %d; ",

                     inventoryModel.iCoNo,
                     inventoryModel.iName,
                     inventoryModel.iUnit,
                     inventoryModel.iInventoryCount,
                     inventoryModel.iCreateUserNo,
                     inventoryModel.iCreateTime,
                     (long)inventoryModel.iIsChecked,
                     inventoryModel.iCheckedTime,
                     inventoryModel.iRemark,
                     inventoryModel.iId
                     ];
    return [[CSFMDBTool SharedFmdbTool] updateTableWithSql:sql];
}

+ (NSArray<NSNumber *> *)updateDbWithInventoryList:(NSArray<TInventoryModel *> *)inventoryList {
    NSMutableArray<NSNumber *> *marr = [NSMutableArray<NSNumber *> array];
    
    for (int i = 0; i < inventoryList.count; i ++) {
        NSInteger dbId = [self updateDbWithInventoryModel:inventoryList[i]];
        [marr addObject:@(dbId)];
    }
    return marr.copy;
}

+ (NSArray<TInventoryModel *> *)loadListWithUserNo:(NSString *)userNo isDiff:(BOOL)isDiff {
    NSString *sql = [NSString stringWithFormat:@"select * from tInventoryInfo where iCreateUserNo = '%@'; ", userNo];
    if (isDiff) {
        return [self loadListDiffDateWithSql:sql]; //区分生产日期
    }else {
        return [self loadListNotDiffDateWithSql:sql]; //不区分生产日期(合并)
    }
}






#pragma mark BLL
    
+ (NSArray<TInventoryModel *> *)loadListDiffDateWithSql:(NSString *)sql {
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr || arr.count == 0) {
        NSLog(@"error: loadListWithUserNo failed!");
        return nil;
    }
    return [self loadListWithDictArr:arr];
}
    
+ (NSArray<TInventoryModel *> *)loadListNotDiffDateWithSql:(NSString *)sql {
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr || arr.count == 0) {
        NSLog(@"error: loadListWithUserNo failed!");
        return nil;
    }
    NSArray<TInventoryModel *> *list = [self loadListWithDictArr:arr];
    return [self processList:list];
}
    
+ (NSArray<TInventoryModel *> *)processList:(NSArray<TInventoryModel *> *)list {
    NSMutableArray<TInventoryModel *> *mList = [NSMutableArray<TInventoryModel *> array];
    [mList addObject:list[0]];
    for (int i = 1; i < list.count; i++) {
        BOOL isIn = NO;
        for (int j = 0; j < mList.count; j++) {
            if ([list[i].iCoNo isEqualToString:mList[j].iCoNo]) {
                isIn = YES;
                TInventoryModel *mo = [CSGXInventoryManagerBLL getInventoryModelWithInventoryList:@[list[i], mList[j]]];
                mList[j] = mo;
                break;
            }
        }
        if (!isIn) {
            [mList addObject:list[i]];
        }
    }
    
    return mList.copy;
}



@end
